display-name: "Games/Crystal Caves"
meta: {"Working": ["All tiles, sprites, and images", "Sound"], "Thanks to": "Frenkel Smeijers"}

if (file.name like "*.mni")
{
	folder: "Fonts"

	unsigned8 tile-count-in-file
	unsigned8 width-in-bytes
	unsigned8 height-in-bytes

	image
	{
		format: "A1I4"
		tile-count: tile-count-in-file
		width: 8
		height: 8	
		
		bit-stride: width
		tiles-across: 25
	}
}

if (file.name like "*.gfx")
{
	folder: "Tiles"

	loop (23)
	{
		unsigned8 sprites-in-chunk
		unsigned8 width-in-bytes
		unsigned8 height-in-bytes

		image
		{
			format: "A1I4"
			width: 16
			height: 16			
			tiles-across: 25
			tile-count: sprites-in-chunk

			bit-stride: 8
		}
	}
}

if (file.name like ["*.cdt", "*.ttl", "*.end", "*.apg"])
{
	folder: "Fullscreen images"
	interpret-as "image-PCX"
}

if (file.name like "*.snd")
{
	folder: "Sound"
	file[12]: i
	{
		name: "Sound " + i
		offset: i * 610
		size: 610
		folder: ""

		interpret-as "cc-sound"
	}
}

file-format cc-sound
{
	at 600
	{
		unsigned16 priority
		unsigned16 unknown0
		unsigned16 vibrate
		unsigned32 unknown1
	}

	compressed[600] "CC-sound"
	{
		sound
		{
			channels: 1
			samples-per-second: 44100
			bits-per-sample: 16
		}
	}
}

function CC-sound
{
	i: 0
	loop
	{
		signed16 freq //in Hz

		if (freq != -1)
		{
			if ((i % vibrate) != 0)
			{ 
				write frequency 344 freq 1193180 255
			}
			else
			{
				write frequency 344 freq 1193180 0
			}
		}

		i = i + 1
	} while ((freq != -1) and (i < 300))
}

level-code:
{
	"cc1.exe": {
		"builder-start": [0xD1AD, 0xD30B, 0xD464, 0xD603, 0xD787,
                                  0xD8F9, 0xDA71, 0xDBD1, 0xDD48, 0xDEB3,
                                  0xE013, 0xE191, 0xE2F1, 0xE45D, 0xE5CF, 
                                  0xE73B, 0xE8A7, 0xEA1F, 0xEB9D],
		"level-data-start": 0x8CE0,
		"first-builder-offset": 0x6EE0
	},

	"cc2.exe": {
		"builder-start": [0xD132, 0xD290, 0xD3E9, 0xD588, 0xD6E8, 0xD860, 
							0xD9D7, 0xDB25, 0xDC91, 0xDE21, 0xDFAA, 0xE11C,
							0xE29A, 0xE411, 0xE57D, 0xE6EF, 0xE84F, 0xE9AF, 0xEB0F],
		"level-data-start": 0x8CE0,
		"first-builder-offset": 0x6EE0
	},

	"cc3.exe": {
		"builder-start": [0xD2E6, 0xD3CC, 0xD501, 0xD6A0, 0xD7FF, 0xD971, 0xDAE3, 0xDC55, 0xDDB5, 0xDF27, 0xE08D,
							0xE1FF, 0xE35F, 0xE4D7, 0xE63D, 0xE7A8, 0xE902, 0xEA50, 0xEBB0],
		"level-data-start": 0x8F24,
		"first-builder-offset": 0x6F24
	}
}

if (file.name like "cc?.exe")
{
	level-data: level-code[file.name]

	file[level-data.builder-start.length]: i
	{
		original-name: file.name
		offset: 0
		size: file.size
		index: i
		name: "Level " + (i - 2)

		episode: file.name[2]
		original-name: file.name

		if (i == 0)
		{
			name = "Intro"
		}
		else if (i == 1)
		{
			name = "Outro"
		}
		else if (i == 2)
		{
			name = "Main"
		}

		//Get level params from machine code
		at 0
		{
			compressed [file.size] "cc-level-get-params"
			{
				read unsigned16 plat-index
				read unsigned16 beam-offset
				read unsigned16 walls-offset
				read unsigned16[25] bg-offset
				read unsigned16[25] bg-repeat
			}
		}

		compressed "cc-level-unpack"
		interpret-as "cc-level" 
	}
}

file-format cc-level
{
	episode: original-name[2]
	level
	{
		grid-x: 16
		grid-y: 16
		width: 40
		height: 24

		data-type:  "unsigned16"

		at 0:
		compressed [file.size] "cc-level-remap-bg"
		{
			level-layer
			{			
				layer-name: "Background"
				layer-image: "cc" + episode + ".gfx"
				ignore-tile: 0xFFFF
			}
		}

		at 0:
		compressed [file.size] "cc-fixup-level"
		{
			compressed [file.size] "cc-level-remap-back"
			{
				level-layer
				{		
					layer-name: "Foreground"	
					layer-image: "cc" + episode + ".gfx"
					ignore-tile: 0xFFFF
				}
			}
		}

		at 0:
		compressed [file.size] "cc-level-remap-front"
		{
			level-layer
			{			
				layer-name: "Foreground"
				layer-image: "cc" + episode + ".gfx"
				ignore-tile: 0
			}
		}
	}
}

cc-level-simple-remap: {
	0x21: 650,
	0x22: 630,
	0x23: 124,
	0x25: 539,
	0x26: 662,
	0x28: 184,
	0x29: 185,
	0x2A: 105,
	0x2C: 537,
	0x2D: 536,
	0x2E: 538,
	0x2F: 480,
	0x38: 34,
	0x39: 96,
	0x3A: 629,
	0x3D: 689,
	0x3F: 52,
	0x30: 43,
	0x41: 882,
	0x42: 6,
	0x43: 1071,
	0x45: 680,
	0x46: 470,
	0x47: 298,
	0x48: 590,
	0x49: 233,
	0x4A: 453,
	0x4B: 1050,
	0x4C: 1051,
	0x4D: 300,
	0x4E: 609,
	0x53: 154,
	0x54: 474,	
	0x56: 594,
	0x57: 150,
	0x5D: 299,
	0x5E: 201,
	0x61: 560,
	0x6A: 605,
	0x6B: 1052,
	0x6C: 1053,
	0x69: 674,
	0x6F: 100,
	0x70: 604,
	0x71: 560,
	0x73: 559,
	0x75: 720,
	0x76: 580,
	0x77: 559,
	0x78: 12,
	0x7C: 184,
	0x7E: 212,
	0x82: 560,
	0x84: 559,
	0x86: 422,
	0x89: 558,
	0x8A: 554,
	0x8B: 3,
	0x8C: 587,
	0x8E: 709,
	0x8F: 712,
	0x90: 386,
	0x91: 499,
	0x92: 476,
	0x94: 378,
	0x93: 477,
	0x95: 379,
	0xA0: 416,
	0xA1: 420,
	0xA2: 418,
	0xA6: 414,
	0xA7: 649,
	0xA8: 643,
	0xA9: 646,
	0xAA: 648,
	0xAB: 644,
	0xAC: 645,
	0xB0: 2,
	0xB1: 598,
	0xB2: 586,
	0xB3: 856,
	0xBA: 583,
	0xBB: 588,
	0xBC: 589,
	0xBD: 579,
	0xBE: 578,
	0xBF: 540,
	0xC0: 544,
	0xC1: 546,
	0xC2: 547,
	0xC5: 536, //crossover
	0xC6: 442,
	0xC7: 443,
	0xCA: 584,
	0xCB: 582,
	0xCD: 590,
	0xCE: 1056,
	0xCF: 543,
	0xD0: 557,
	0xD1: 542,
	0xD5: 639,
	0xD6: 594,
	0xD7: 594,
	0xD8: 581,
	0xD9: 545,
	0xDA: 541,
	0xDC: 609,
	0xE7: 548,
	0xE8: 394,
	0xE9: 395,
	0xEA: 396,
	0xEB: 397,
	0xEC: 398,
	0xED: 399,
	0xF0: 852,
	0xF3: 1044,
	0xF4: 8,
	0xF5: 10,
	0xF6: 182,
	0xF7: 183,
	0xF9: 553,
	0xFA: 561,
	0xFB: 585,
	0xFC: 498,
	0xFD: 499,
	0xFE: 476,
	["*"]: -1
}

wall-remap: 
{"1": [0, 0, 0, 1082, 1070, 0, 1070, 1070, 0, 970, 1112, 0, 0, 1112, 1112, 1062, 970, 758, 0],
 "2": [0, 0, 0, 1070, 0, 1070, 0, 0, 0, 1100, 0, 1112, 1082, 1070, 1100, 1082, 0, 0, 1062],
 "3": [0, 0, 0, 0, 0, 758, 1100, 0, 758, 1112, 1070, 1082, 970, 1082, 0, 0, 0, 1100, 1070]}

wall-index: {
	0x66: 4,
	0x67: 5,
	0x68: 6,
	0x34: 8,
	0x35: 9,
	0x36: 10,
	0x72: 0,
	0x74: 1,
	0x79: 2
}

gem-remap: {
	"1": 600,
	"2": 612,
	"3": 632
}

gem-index: {
	0x52: 0,
	0x2B: 1,
	0x62: 2,
	0x63: 3
}

smallplat-remap: {
	"1": [0, 0, 0, 950, 950, 0,   950, 951, 0, 951, 0,   950, 0,   0,   952, 0, 0,   0,   0],
	"2": [0, 0, 0, 950, 0,   0,   950, 0,   0, 0,   950, 952, 952, 951, 951, 0, 951, 950, 952],
	"3": [0, 0, 0, 950, 951, 951, 952, 951, 0, 0,   952, 950, 950, 0,   0,   0, 0,   0,   0]
}

smallplat-index: {
	0x5F: 0
}

beam-remap: {
	"1": [39, 39, 0, 956, 953, 0, 0, 956, 0, 0, 959, 953, 0, 0, 959, 0, 0, 0, 0],
	"2": [39, 39, 0, 953, 956, 0, 0, 0, 0, 0, 953, 959, 959, 956, 959, 0, 0, 0, 953],
	"3": [0, 0, 0, 0, 0, 956, 0, 0, 0, 0, 0, 0, 953, 0, 0, 0, 953, 0, 0]
}

beam-index: {
	0x44: 0,
	0x64: 1,
	0x98: 0,
	0x99: 1,
	0x9A: 2
}

verts: {
	0x24: 860,
	0x80: 858,
	0x85: 427,
	0x87: 0,
	0x88: 1,
	0xA3: 424,
	0xA4: 426,
	0xA5: 425,
	0xC3: 1057,
	0xC4: 1061,
	0xE0: 850,
	0xF2: 982,
	0xF8: 866,
	0x9F: 874
}

function cc-level-remap-bg
{
	//Set up for 0x5A tile
	tile-5a-2a6: 0 //not sure of starting value
	tile-5a-2a8: 0 //not sure of starting value

	loop(24): row
	{
		if (bg-repeat[row] > 1)
		{
			loop (40): col
			{
				odd-row: 4 * (row % 2)
				write unsigned16 bg-offset[row] + (col % (bg-repeat[row] / 2)) + odd-row
			}
		}
		else
		{
			loop (40): col
			{
				//Not correct, but close
				uVar4: tile-5a-2a6
				iVar1: tile-5a-2a8
				lVar2: (uVar4 * 0x2607) & 0xFFFF
				uVar3: /*(uint)*/lVar2
				uVar4 = uVar4 << 3
				//uVar4 = (int)((ulong)lVar2 >> 0x10) + (uVar4 & 0xff | (uint)(byte)((char)(uVar4 >> 8) + (char)uVar4) << 8) + iVar1 * 5;
				uVar4 = (lVar2 >> 0x10) + ((uVar4 & 0xFF) | (((uVar4 >> 8) & 0xFF) + ((uVar4 & 0xFF) << 8))) + (iVar1 * 5)
				tile-5a-2a6 = uVar3 + 1
				//*(int *)0x2a8 =
				//   (uVar4 & 0xff | (uint)(byte)((char)(uVar4 >> 8) + (char)iVar1 * 4 + (char)(iVar1 << 7)) <<8)
				//   + (uint)(0xfffe < uVar3);
				tile-5a-2a8 = ((uVar4 & 0xFF) | ((((uVar4 >> 8) & 0xFF) + ((iVar1 * 4) & 0xFF) + ((iVar1 << 7) & 0xFF)) << 8)) + (0xfffe < uVar3)

				if (uVar4 < 0)
				{
					uVar4 = -uVar4
				}

				if ((index == 2) and (row == 4))
				{
					if ((tile-5a-2a8 % 5) == 4)
					{
						write unsigned16 232
					}
					else
					{
						write unsigned16 (239 + (tile-5a-2a8 % 5))
					}
				}
				else
				{
					tile: tile-5a-2a8
					if ((tile-5a-2a8 % 8) == 4)
					{
						//No idea, CC does something similar.  It's all random, anyway.
						mod: (tile-5a-2a8 >> 8) & 0xFF
						check: 32
						if (index == 2)
						{
							check = 64
						}
						if (mod > check)
						{
							tile = tile + 1
						}
					}

					write unsigned16 (342 + (tile % 8))
				}
			}
		}
	}
}

function cc-level-get-params
{
	level-data: level-code[original-name]

	//Get wall color
	at level-data.builder-start[index]:

	smallplat: 950
	beam: 949
	walls-718: 0
	walls-716: 0
	bg-696: [](25)
	bg-20770: [](25)
	bg-repeat: [](25)

	searching: true
	loop while (searching)
	{
		x86opcode op
		
		/*if (op like "mov ax,*")
		{
			at file.position - 2:
			unsigned16 row
			current-row = row
		}
		else */if (op like "jmp*")
		{
			searching = false
		}
		else if (op like "mov word 696, *")
		{
			at file.position - 2:
			read unsigned16 bg-696-index
			bg-696[0] = bg-696-index
		}
		else if (op like "mov word 20770, *")
		{
			at file.position - 2:
			read unsigned16 bg-20770-index
			bg-20770[0] = bg-20770-index
		}
		else if (op like "mov word 728, *")
		{
			at file.position - 2:
			read unsigned16 this-plat-index
			smallplat = 949 + this-plat-index
		}
		else if (op like "mov word 724, *")
		{
			at file.position - 2:
			read unsigned16 this-plat-index
			beam = 949 + this-plat-index
		}
		else if (op like "mov word 716, *")
		{
			at file.position - 2:
			read unsigned16 walls-716-index
			walls-716 = walls-716-index
		}
		else if (op like "mov word 718, *")
		{
			at file.position - 2:
			read unsigned16 walls-718-index
			walls-718 = walls-718-index
		}
		else if (op like "mov word 20766, *")
		{
			at file.position - 2:
			read unsigned16 bg-repeat-index
			bg-repeat[0] = bg-repeat-index
		}
	}

	write unsigned16 smallplat
	write unsigned16 beam
	write unsigned16 (walls-718 * 50) + walls-716 - 51

	//Per row :)
	loop (25)
	{
		write unsigned16 (bg-696[0] * 50) + bg-20770[0] - 51
	}

	loop (25): i
	{
		if ((index == 2) and (i < 5))
		{
			write unsigned16 0
		}
		else
		{
			write unsigned16 bg-repeat[0]
		}
	}
}

function cc-level-remap-back
{
	data(40) unknown //first row is discarded

	last-tile: 0
	n-markers: [](file.size)

	episode-hack: episode

	loop while (file.remaining-bytes > 0)
	{
		read unsigned8 this-tile

		simple-remap: cc-level-simple-remap[this-tile]
		if (simple-remap != -1)
		{
			if (this-tile == 0x57)
			{
				if ((file.position % 40) < 20)
				{
					simple-remap = simple-remap + 2
				}
			}
			write unsigned16 simple-remap
			last-tile = simple-remap

			if (last-tile == 604) //fix crumbly platform
			{
				last-tile = 605
			}
			if (this-tile == 0x69)
			{
				n-markers[file.position - 1 + 40] = 675
			}
			if (this-tile == 0x86)
			{
				n-markers[file.position - 1 + 40] = 423
			}
		}
		else if (this-tile like [0x34, 0x35, 0x36, 0x66, 0x67, 0x68, 0x72, 0x74, 0x79])
		{
			write unsigned16 (walls-offset + wall-index[this-tile])
		}
		else if ((this-tile == 0xDB) or (this-tile == 0x6D))
		{
			remap-tile: 608
			if (episode-hack == "3")
			{
				if ((index == 1) or (index == 2))
				{
					remap-tile = 607
				}
			}

			write unsigned16 remap-tile
			last-tile = remap-tile
		}
		else if (this-tile == 0x5F)
		{
			write unsigned16 plat-index
		}
		else if (this-tile like [0x52, 0x2B, 0x62, 0x63])
		{
			write unsigned16 (gem-remap[episode] + gem-index[this-tile])
		}
		else if (this-tile == 0x55) //hammer
		{
			//Output rail x 2
			write unsigned16 474
			read unsigned8 ignore
			write unsigned16 475	

			//Mark 'n's for next row
			n-markers[file.position - 2 + 40] = 464
			n-markers[file.position - 1 + 40] = 465
		}
		else if (this-tile like [0x44, 0x64, 0x98, 0x99, 0x9A])
		{
			last-tile = (beam-offset + beam-index[this-tile])

			if (index < 2)
			{
				last-tile = 0xFFFF
			}
			write unsigned16 last-tile

			if (index < 2)
			{
				last-tile = 0xFFFE
			}

			//Check for 2-long case :/
			if (beam-index[this-tile] == 0)
			{
				unsigned8 check1
				unsigned8 check2

				if ((check1 == 0x6E) and ((check2 == 0x20) or (check2 == 0x47))) //-x47 is a hack, should be checking for not beam instead
				{
					last-tile = last-tile + 1
				}

				at file.position - 2:
			}
		}
		else if (this-tile == 0x5B) //sign
		{
			read unsigned8 sign-id
			if (sign-id == 0x54)
			{
				last-tile = 388
				n-markers[file.position - 1 + 40] = last-tile - 2

				write unsigned16 last-tile
				last-tile = last-tile - 1
				write unsigned16 last-tile
				last-tile = last-tile + 1
			}
			else if (sign-id == 0x79)
			{
				last-tile = 400
				n-markers[file.position - 2 + 40] = last-tile + 4
				n-markers[file.position - 1 + 40] = last-tile + 7
				write unsigned16 last-tile
				last-tile = last-tile + 3
				write unsigned16 last-tile
			}
			else if (sign-id == 0x3D)
			{
				last-tile = 827
				write unsigned16 last-tile
				write unsigned16 (last-tile + 4)

				n-markers[file.position] = last-tile + 8
			}
			else if (sign-id == 0x45)
			{
				last-tile = 1007
				write unsigned16 last-tile
				write unsigned16 (last-tile - 7)

				n-markers[file.position] = last-tile + 7
			}
			else if (sign-id == 0x50)
			{
				last-tile = 736
				write unsigned16 last-tile
				write unsigned16 (last-tile + 2)

				n-markers[file.position] = last-tile + 3
			}
			else
			{
				if ((sign-id == 0x44) or (sign-id == 0x64))
				{
					last-tile = 458
				}
				else if (sign-id == 0x2A)
				{
					last-tile = 432
					n-markers[file.position - 2 + 40] = last-tile + 4
				}
				else if (sign-id == 0x23)
				{
					last-tile = 700
					n-markers[file.position - 2 + 40] = last-tile + 4
				}
				else if (sign-id == 0x2D)
				{
					last-tile = 90
				}
				else if (sign-id == 0x31)
				{
					last-tile = 454
				}
				else if (sign-id == 0x32)
				{
					last-tile = 456
				}
				else if (sign-id == 0x34)
				{
					last-tile = 450
				}
				else if (sign-id == 0x35)
				{
					last-tile = 524
				}
				else if (sign-id == 0x5D)
				{
					last-tile = 144
					n-markers[file.position - 2 + 40] = last-tile + 4
				}
				else if (sign-id == 0x5E)
				{
					last-tile = 92
				}
				else if (sign-id == 0x66)
				{
					last-tile = 142
				}
				else if (sign-id == 0x6D)
				{
					last-tile = 243
				}
				else if (sign-id == 0x7C)
				{
					last-tile = 17
				}
				else if (sign-id == 0x72)
				{
					last-tile = 140
				}
				else if (sign-id == 0x63)
				{
					last-tile = 336
					n-markers[file.position - 2 + 40] = last-tile + 4
				}
				else if (sign-id == 0x78)
				{
					last-tile = 186
					n-markers[file.position - 2 + 40] = last-tile + 2
				}
				else if (sign-id == 0x41)
				{
					last-tile = 510
					n-markers[file.position - 2 + 40] = last-tile + 4
				}
				else if (sign-id == 0x4F)
				{
					last-tile = 434
					n-markers[file.position - 2 + 40] = last-tile + 4
				}
				else if (sign-id == 0x42)
				{
					last-tile = 526
					n-markers[file.position - 2 + 40] = 514
				}
				else if (sign-id == 0x67)
				{
					last-tile =  400
					n-markers[file.position - 2 + 40] = last-tile + 4

					n-markers[file.position] = last-tile + 3
					n-markers[file.position + 40] = last-tile + 7
				}
				else if (sign-id == 0x62)
				{
					last-tile =  400
					n-markers[file.position - 2 + 40] = last-tile + 4
				}
				else if (sign-id == 0x3A)
				{
					last-tile =  1054
				}
				else if (sign-id == 0x33)
				{
					last-tile = 462
				}
				else if (sign-id == 0x3B)
				{
					last-tile =  1058
				}
				else
				{
					last-tile = 46 //debug
				}

				write unsigned16 last-tile
				last-tile = last-tile + 1
				write unsigned16 last-tile
			}
		}
		else if (this-tile == 0x58) //exit
		{
			write unsigned16 562
			last-tile = 569

			n-markers[file.position - 1 + 40] = 566
		}
		else if (this-tile like [0x24, 0x80, 0x85, 0x87, 0x88, 0xA3, 0xA4, 0xA5, 0xC3, 0xC4, 0xE0, 0xF2, 0xF8, 0x9F])
		{
			last-tile = verts[this-tile]
			write unsigned16 last-tile

			if (this-tile == 0xC3)
			{
				last-tile = verts[0xC4]
			}
			n-markers[file.position - 1 + 40] = last-tile + 4
		}
		else if (this-tile == 0x6E)
		{
			if (n-markers[file.position - 1] != 0)
			{
				last-tile = n-markers[file.position - 1]
			}
			else
			{
				last-tile = last-tile + 1
			}

			write unsigned16 last-tile
		}
		else if (this-tile == 0x59) //player
		{
			if (index < 2)
			{
				write unsigned16 516
			}
			else
			{
				write unsigned16 250
			}
		}
		else
		{
			if ((this-tile != 0x20) and (this-tile != 0xC8) and (this-tile != 0x7A) and (this-tile != 0x5A))
			{
				write unsigned16 46 //debug
			}
			else
			{
				write unsigned16 0xFFFF
			}
		}
	}
}

function cc-level-remap-front
{
	data(40) unknown //first row is discarded

	loop while (file.remaining-bytes > 0)
	{
		read unsigned8 this-tile

		if (this-tile == 0xC5)
		{
			write unsigned16 539
		}
		else if (this-tile == 0x98)
		{
			write unsigned16 636
		}
		else if (this-tile == 0x99)
		{
			write unsigned16 636
		}
		else if (this-tile == 0x9A)
		{
			write unsigned16 636
		}
		else if (this-tile == 0x55)
		{
			write unsigned16 460
			read unsigned8 ignore
			write unsigned16 461
		}
		else
		{
			write unsigned16 0
		}
	}
}

function cc-level-unpack
{
	//Get rows
	at level-data.builder-start[index]:

	rows: [](25)

	empty-row: [](40)
	loop (40): i
	{
		empty-row[i] = 0x20
	}
	loop (25): i
	{
		rows[i] = empty-row
	}

	current-row: 0
	running: true

	loop while (running)
	{
		x86opcode op
		if (op like "mov ax,*")
		{
			at file.position - 2:
			unsigned16 row
			current-row = row
		}
		else if (op like "mov di,*")
		{
			at file.position - 2:
			unsigned16 off
			at (level-data.level-data-start - level-data.first-builder-offset + off + 1)
			{
				read unsigned8[0x28] line
				rows[current-row] = line
			}
		}
		else if (op like "jmp*")
		{
			running = false
		}
		/*else if (op like "mov word *")
		{
			log (op)
		}*/
	}

	loop (25): i
	{
		write unsigned8 rows[i]
	}
}

function cc-fixup-level
{
	//Fix 0xF2 monster and downgrowths
	rows: [](25)

	loop (25): i
	{
		read unsigned8[0x28] line
		rows[i] = line
	}

	loop (25): i
	{
		loop(40): j
		{
			if (rows[24 - i][j] == 0xF2)
			{
				rows[24 - i][j] = 0x6E
				rows[24 - i - 1][j] = 0x1F2
			}
			if (rows[24 - i][j] like [0xC4, 0x85, 0x86, 0x87, 0x88])
			{
				if ((rows[24 - i + 1][j] != rows[24 - i][j]) and (rows[24 - i + 1][j] != 0x6E))
				{
					rows[24 - i - 1][j] = rows[24 - i][j]
					rows[24 - i][j] = 0x6E
				}
				if ((rows[24 - i - 1][j] == 0x6E) or (rows[24 - i - 1][j] == 0x20))
				{
					rows[24 - i - 1][j] = rows[24 - i][j]
				}
			}
		}
	}

	loop (25): i
	{
		loop(40): j
		{
			rows[i][j] = rows[i][j] & 0xFF
		}
	}

	loop (25): i
	{
		write unsigned8 rows[i]
	}
}